package com.emm.yixun.mobile.ui.customer;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.copy_AttenAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetPremisesList;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;
import java.util.Map;

import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b> 意向组团
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:05:22
 */
public class GroupActivity extends BaseActivity {

	private static final String TAG = "GroupActivity";

	/** 意向组团 */
	TextView title_main;

	/** 返回 */
	ImageView back_btn;

	/** 组团列表 */
	ListView swip_list;
	copy_AttenAdapter adapter;

	/** 取消 */
	Button cancel_btn;

	/** 确定 */
	Button sure_btn;

	GetPremisesList getpremises;

	/** 侧滑关闭框架 */
//	private SwipeBackLayout mSwipeBackLayout;

	
	@Override
	protected void onResume() {
		BaseActivity.activity = this;
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.attention_activity);
		
//		SetSwipeBackLayout(mSwipeBackLayout);
		
		BaseActivity.activity = this;

		initView();

		SetRelativiLayoutPadding(R.id.main_head_top_rela);
		
		initBtn();
		getPremisesList();
	}

	/**
	 * 
	 * <p><b>Title：</b> 初始化view</p>
	 * <p><b>Description：</b> </p>
	 * @author tangxin
	 * @date 2016年12月7日 下午3:27:06
	 */
	private void initView() {
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(getString(R.string.title_group));
		back_btn = (ImageView) findViewById(R.id.back_btn);
		back_btn.setVisibility(View.GONE);
		cancel_btn = (Button) findViewById(R.id.cancel_btn);
		sure_btn = (Button) findViewById(R.id.sure_btn);
		swip_list = (ListView) findViewById(R.id.listView_s);
	}

	/**
	 * 
	 * <p><b>Title：</b> 初始化监听事件</p>
	 * <p><b>Description：</b> </p>
	 * @author tangxin
	 * @date 2016年12月7日 下午3:27:13
	 */
	private void initBtn() {
		sure_btn.setVisibility(View.VISIBLE);
		sure_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (null != getpremises && null != getpremises.getPremisesList()) {
					EmmApplication.grouplist.clear();
					for (int i = 0; i < getpremises.getPremisesList().size(); i++) {
						if (getpremises.getPremisesList().get(i).isIsselecet()) {
							EmmApplication.grouplist.add(getpremises.getPremisesList().get(i));
						}
					}
				}
				finish();
			}
		});
		cancel_btn.setVisibility(View.VISIBLE);
		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		swip_list.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Log.w(TAG, "a--" + position);
				getpremises.getPremisesList().get(position)
						.setIsselecet(getpremises.getPremisesList().get(position).isIsselecet() == true ? false : true);
				adapter.GetDate(getpremises.getPremisesList());
			}
		});

	}

	/**
	 * 
	 * <p><b>Title：</b> 意向组团列表查询接口</p>
	 * <p><b>Description：</b> </p>
	 * @author tangxin
	 * @date 2016年12月7日 下午3:27:27
	 */
	private void getPremisesList() {
		EmmApplication.updateUrl("getPremisesList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getPremisesList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.showDialogForLoading(GroupActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getpremises = JSONObject.parseObject(resData.toString(), GetPremisesList.class);

				if ("1".equals(getpremises.getResult())) {
					Log.v("获取成功", "");
					if (getpremises.getPremisesList() != null) {

						if (EmmApplication.grouplist.size() > 0) {
							for (int t = 0; t < EmmApplication.grouplist.size(); t++) {
								for (int y = 0; y < getpremises.getPremisesList().size(); y++) {
									if (EmmApplication.grouplist.get(t).getPremisesId()
											.equals(getpremises.getPremisesList().get(y).getPremisesId())) {
										getpremises.getPremisesList().get(y).setIsselecet(true);
									}
								}
							}
						}
						adapter = new copy_AttenAdapter(GroupActivity.this, getpremises.getPremisesList());
						swip_list.setAdapter(adapter);
					} else {
						EmmApplication.T("列表内容为空");
					}
				} else {
					EmmApplication.T(getpremises.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getpremises.getErrorCode().toString() + "errorMsg:" + getpremises.getErrorMsg().toString());
				}
			}
		});
	}
}
